package com.gxa.controller;

import com.gxa.common.PaginationResult;
import com.gxa.common.ResponseData;
import com.gxa.pojo.Announcement;
import com.gxa.pojo.Employee;
import com.gxa.service.AnnService;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zrf
 * Date: 2020/10/26 21:50
 * Description:公告
 * Version: V1.0
 */
@Controller
@RequestMapping("/ann")
@Api(value = "公告信息",tags = "公告信息接口API")
public class AnnController {
    @Autowired
    private AnnService annService;
    @Autowired
    private ResponseData responseData;

    /**
     * 查询所有未发布公告
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/queryAnnListUn",method = RequestMethod.GET)
    @ApiOperation(value = "查询未发布信息并且做分页",notes = "查询未发布公告并且做分页",httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示条数",paramType = "query"),
    })
    @ResponseBody
    public ResponseData queryAnnListUn(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize) {
        System.out.println(currentPage + "  " + pageSize);
        PaginationResult paginationResult = annService.selAnnUn(currentPage,pageSize);
        System.out.println(paginationResult);
        if(paginationResult != null){
            responseData.setMsg("成功");
            responseData.setCode(200);
            responseData.setData(paginationResult);
        }else{
            responseData.setMsg("失败");
            responseData.setCode(400);
            responseData.setData(null);
        }

        return responseData;
    }
    /**
     * 查询所有已经发布公告
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/selAnn",method = RequestMethod.GET)
    @ApiOperation(value = "查询已经发布信息并且做分页",notes = "查询已经发布公告并且做分页",httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页码数",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示条数",paramType = "query"),
    })
    @ResponseBody
    public ResponseData selAnn(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize){
        System.out.println(currentPage + "  " + pageSize);
        PaginationResult paginationResult = annService.selAnn(currentPage,pageSize);
        System.out.println(paginationResult);
        if(paginationResult != null){
            responseData.setMsg("成功");
            responseData.setCode(200);
            responseData.setData(paginationResult);
        }else{
            responseData.setMsg("失败");
            responseData.setCode(400);
            responseData.setData(null);
        }

        return responseData;
    }
    /**
     * 添加公告
     *
     * @param announcementTitle,announcementDescribe
     * @return ResponseData
     */
    @RequestMapping(value = "/addAnn",method = RequestMethod.POST)
    @ApiOperation(value = "添加公告",notes = "添加一个公告",httpMethod = "post", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "announcementTitle",value = "公告标题",paramType = "query"),
            @ApiImplicitParam(name = "announcementDescribe",value = "公告内容",paramType = "query"),
            @ApiImplicitParam(name = "employeeId",value = "员工编码",paramType = "query"),
    })
    @ResponseBody
    public ResponseData addAnn(@RequestParam String announcementTitle,@RequestParam String announcementDescribe, @RequestParam Integer employee_id){
        System.out.println(announcementTitle+" "+announcementDescribe+" "+employee_id);
        int reulet = annService.addAnn(announcementTitle,announcementDescribe,employee_id);
        if(reulet > 0){
            responseData.setData(null);
            responseData.setCode(200);
            responseData.setMsg("添加成功");
            return responseData;
        }else{
            responseData.setData(null);
            responseData.setCode(400);
            responseData.setMsg("添加失败");
            return responseData;
        }

    }
    /**
     * 发布公告
     *
     * @param announcementId
     * @return ResponseData
     */
    @RequestMapping(value = "/updateAnn",method = RequestMethod.POST)
    @ApiOperation(value = "发布公告",notes = "根据announcementId发布公告",httpMethod = "post", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "announcementId",value = "公告id",paramType = "query"),
    })
    @ResponseBody
    public ResponseData updateAnn(@RequestParam Integer announcementId){
        System.out.println(announcementId);
        int result = annService.updateAnnId(announcementId);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("发布成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("发布失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 编辑还未发布的公告,
     *
     * @param
     * @return ResponseData
     */
    @RequestMapping(value = "/updateAnnNot",method = RequestMethod.POST)
    @ApiOperation(value = "编辑还未发布的公告",notes = "根据announcementId编辑未发布公告",httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData updateAnnNot(@RequestBody @ApiParam(name = "announcement", value = "公告",required = true) Announcement announcement){
        System.out.println(announcement.toString());
        int result = annService.updateAnn(announcement);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("编辑成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("编辑失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 删除未发布的公告
     *
     * @param announcementId
     * @return ResponseData
     */
    @RequestMapping(value = "/delAnnByAnnId",method = RequestMethod.POST)
    @ApiOperation(value = "删除未发布的公告",notes = "根据announcementId删除未发布的公告",httpMethod = "post", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "announcementId",value = "公告id",paramType = "query"),
    })
    @ResponseBody
    public ResponseData delAnnByAnnId(@RequestParam int announcementId){
        System.out.println(announcementId);
        int result = annService.delAnnById(announcementId);
        if (result>0){
            responseData.setMsg("删除成功");
            responseData.setCode(200);
            responseData.setData(null);
        }else {
            responseData.setMsg("删除失败");
            responseData.setCode(400);
            responseData.setData(null);
        }
        return responseData;
    }
}
